<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars
define("PRIMARY_KEY","ktrasl");
$FileName      = "ais_setup_company_param.php";
$FileTemplate  = "ais_setup_company_param_form.html";
$SubHeaderTitle= "Setup Company Parameter";
$TableName     = "$DBName.tblprofile";
$HTMLFormName  = "ProfileForm";

$MaxGrid        = "40";

$InitFunction = "Masking();Masking2();";

$FormAction    = $FileName . "?" . AddParam(GetQueryString("QueryString", ""),"form","$HTMLFormName");
$FormEncType   = "application/x-www-form-urlencoded";
//$PMode         = "";

//Initiate Database Table For Insert & Update;

//modified by dyah. 23 juli 2009. added field kodecbg_pjk
$FormFields    = array("salrl","salrlytd","selkurs","skdstm","nodetbank","apaging","araging","ppn_debtur","ppn_skada","adm_skada","nodetkas","nomemo","nomemx","nodetnd","bs","totnd","nodetnk","totnk","ttdkas3","ttdkas2","ttdkas1","ttdagh2","ttdagh1","ttdagp2","ttdagp1","ttdmm","ttdmb","ttdmacc","ttdkw","ttdtktb2","ttdtktb1","jatahtktb1","jatahtktb2","ttdbkbb2","ttdbkbb1","jatahbkbb1","jatahbkbb2","ttd_setuju_jo_kur","ttd_setuju_jo_lbh","ttd_tgjwb_jo_kur","ttd_tgjwb_jo_lbh","ttd_setuju_survey_kur","ttd_setuju_survey_lbh","ttd_tgjwb_survey_kur","ttd_tgjwb_survey_lbh","ttd_setuju_oth_kur","ttd_setuju_oth_lbh","ttd_tgjwb_oth_kur","ttd_tgjwb_oth_lbh","ttdcn","ttddn","ttdbankdn","taxprefix","bankdn","taxcode","kodecbg_pjk","bag_operasi","bag_traffic","manager_marketing","manager_operasi","manager_keuangan", "dm_hutang", "dm_piutang");
buildsqlfield ("$TableName", $FormFields);

//Leave Here Untouch
$FormCName     = BuildHTMLField($FormFields,"name");
$FormCValue    = BuildHTMLField($FormFields,"value");

$SQLGetProfileId = "SELECT profile_id FROM ".$TableName;
$arrProfileId = $DBConnection->dbc->get_row($SQLGetProfileId,ARRAY_A);

//fetch profile ID
$profileId = $arrProfileId['profile_id'];





//************ FORM ACTION **************
if (GetParam("form","")=="$HTMLFormName") {
    
    if (GetParam("Action","")=="Add") {		
        //FormInsert();  
        /*
		$DBConnection->add_data($TableName,"ledger",GetParam("ledger",""));
		$DBConnection->add_data($TableName,"prsh",GetParam("prsh",""));
		$DBConnection->add_data($TableName,"perush",GetParam("perush",""));
		$DBConnection->add_data($TableName,"perusha",GetParam("perusha",""));
		$DBConnection->add_data($TableName,"npwp",GetParam("npwp",""));
		$DBConnection->add_data($TableName,"ktrasl",GetParam("ktrasl",""));
		$DBConnection->add_data($TableName,"ktrnam",GetParam("ktrnam",""));
		$DBConnection->add_data($TableName,"acc_manager",GetParam("acc_manager",""));
		$DBConnection->add_data($TableName,"treasury_manager",GetParam("treasury_manager",""));
		$DBConnection->add_data($TableName,"addr1",GetParam("addr1",""));
		$DBConnection->add_data($TableName,"addr2",GetParam("addr2",""));
		$DBConnection->add_data($TableName,"addr3",GetParam("addr3",""));
		$DBConnection->add_data($TableName,"phone",GetParam("phone",""));
		$DBConnection->add_data($TableName,"fax",GetParam("fax",""));
		$DBConnection->add_data($TableName,"nodetbank",GetParam("nodetbank",""));
		$DBConnection->add_data($TableName,"nodetkas",GetParam("nodetkas",""));
		$DBConnection->add_data($TableName,"nodetnd",GetParam("nodetnd",""));
		$DBConnection->add_data($TableName,"totnd",GetParam("totnd",""));
		$DBConnection->add_data($TableName,"nodetnk",GetParam("nodetnk",""));
		$DBConnection->add_data($TableName,"totnk",GetParam("totnk",""));
		$DBConnection->add_data($TableName,"nomemo",GetParam("nomemo",""));
		$DBConnection->add_data($TableName,"nomemx",GetParam("nomemx",""));
		*/
		$DBConnection->add_data($TableName,"salrl",GetParam("salrl",""));
		$DBConnection->add_data($TableName,"salrlytd",GetParam("salrlytd",""));
		$DBConnection->add_data($TableName,"skdstm",GetParam("skdstm",""));
		$DBConnection->add_data($TableName,"skdgrphut",GetParam("skdgrphut",""));
		$DBConnection->add_data($TableName,"skdngrhut",GetParam("skdngrhut",""));
		$DBConnection->add_data($TableName,"taxcode",GetParam("taxcode",""));
		$DBConnection->add_data($TableName,"taxprefix",GetParam("taxprefix",""));
		$DBConnection->add_data($TableName,"tglpkp",GetParam("tglpkp",""));
		$DBConnection->add_data($TableName,"selkurs",GetParam("selkurs",""));
		$DBConnection->add_data($TableName,"bs",GetParam("bs",""));
		$DBConnection->add_data($TableName,"kw",GetParam("kw",""));
		$DBConnection->add_data($TableName,"email",GetParam("email",""));
		$DBConnection->add_data($TableName,"url",GetParam("url",""));
		$DBConnection->add_data($TableName,"ttdbs1",GetParam("ttdbs1",""));
		$DBConnection->add_data($TableName,"ttdbs2",GetParam("ttdbs2",""));
		$DBConnection->add_data($TableName,"ttdbs3",GetParam("ttdbs3",""));
		$DBConnection->add_data($TableName,"ttd_setuju_jo_kur",GetParam("ttd_setuju_jo_kur",""));
		$DBConnection->add_data($TableName,"ttd_setuju_jo_lbh",GetParam("ttd_setuju_jo_lbh",""));
		$DBConnection->add_data($TableName,"ttd_tgjwb_jo_kur",GetParam("ttd_tgjwb_jo_kur",""));
		$DBConnection->add_data($TableName,"ttd_tgjwb_jo_lbh",GetParam("ttd_tgjwb_jo_lbh",""));
		$DBConnection->add_data($TableName,"ttd_setuju_survey_kur",GetParam("ttd_setuju_survey_kur",""));
		$DBConnection->add_data($TableName,"ttd_setuju_survey_lbh",GetParam("ttd_setuju_survey_lbh",""));
		$DBConnection->add_data($TableName,"ttd_tgjwb_survey_kur",GetParam("ttd_tgjwb_survey_kur",""));
		$DBConnection->add_data($TableName,"ttd_tgjwb_survey_lbh",GetParam("ttd_tgjwb_survey_lbh",""));
		$DBConnection->add_data($TableName,"ttd_setuju_oth_kur",GetParam("ttd_setuju_oth_kur",""));
		$DBConnection->add_data($TableName,"ttd_setuju_oth_lbh",GetParam("ttd_setuju_oth_lbh",""));
		$DBConnection->add_data($TableName,"ttd_tgjwb_oth_kur",GetParam("ttd_tgjwb_oth_kur",""));
		$DBConnection->add_data($TableName,"ttd_tgjwb_oth_lbh",GetParam("ttd_tgjwb_oth_lbh",""));
		$DBConnection->add_data($TableName,"ttdcn",GetParam("ttdcn",""));
		$DBConnection->add_data($TableName,"ttddn",GetParam("ttddn",""));
		$DBConnection->add_data($TableName,"bankdn",GetParam("bankdn",""));
		$DBConnection->add_data($TableName,"ttdkw",GetParam("ttdkw",""));
		$DBConnection->add_data($TableName,"ttdmm",GetParam("ttdmm",""));
		$DBConnection->add_data($TableName,"ttdmb",GetParam("ttdmb",""));
		$DBConnection->add_data($TableName,"ttdmacc",GetParam("ttdmacc",""));
		$DBConnection->add_data($TableName,"jabtax",GetParam("jabtax",""));
		$DBConnection->add_data($TableName,"araging",GetParam("araging",""));
		$DBConnection->add_data($TableName,"apaging",GetParam("apaging",""));
		$DBConnection->add_data($TableName,"ttdkas1",GetParam("ttdkas1",""));
		$DBConnection->add_data($TableName,"ttdkas2",GetParam("ttdkas2",""));
		$DBConnection->add_data($TableName,"ttdkas3",GetParam("ttdkas3",""));
		$DBConnection->add_data($TableName,"ttdagh1",GetParam("ttdagh1",""));
		$DBConnection->add_data($TableName,"ttdagh2",GetParam("ttdagh2",""));
		$DBConnection->add_data($TableName,"ttdagp1",GetParam("ttdagp1",""));
		$DBConnection->add_data($TableName,"ttdagp2",GetParam("ttdagp2",""));
		$DBConnection->add_data($TableName,"ttdtktb1",GetParam("ttdtktb1",""));
		$DBConnection->add_data($TableName,"ttdtktb2",GetParam("ttdtktb2",""));
		$DBConnection->add_data($TableName,"jatahtktb1",GetParam("jatahtktb1",""));
		$DBConnection->add_data($TableName,"jatahtktb2",GetParam("jatahtktb2",""));
		$DBConnection->add_data($TableName,"ttdbkbb1",GetParam("ttdbkbb1",""));
		$DBConnection->add_data($TableName,"ttdbkbb2",GetParam("ttdbkbb2",""));
		$DBConnection->add_data($TableName,"jatahbkbb1",GetParam("jatahbkbb1",""));
		$DBConnection->add_data($TableName,"jatahbkbb2",GetParam("jatahbkbb2",""));
		$DBConnection->add_data($TableName,"ppn_debtur",GetParam("ppn_debtur",""));
		$DBConnection->add_data($TableName,"ppn_skada",GetParam("ppn_skada",""));
		$DBConnection->add_data($TableName,"adm_skada",GetParam("adm_skada",""));
		$DBConnection->add_data($TableName,"ttnbankdn",GetParam("ttnbankdn",""));
		$DBConnection->add_data($TableName,"ttdbankdn",GetParam("ttdbankdn",""));
		$DBConnection->add_data($TableName,"kodecbg_pjk",GetParam("kodecbg_pjk",""));
		$DBConnection->add_data($TableName,"bag_operasi",GetParam("bag_operasi",""));
		$DBConnection->add_data($TableName,"bag_traffic",GetParam("bag_traffic",""));
		$DBConnection->add_data($TableName,"manager_marketing",GetParam("manager_marketing",""));
		$DBConnection->add_data($TableName,"manager_operasi",GetParam("manager_operasi",""));
		$DBConnection->add_data($TableName,"manager_keuangan",GetParam("manager_keuangan",""));
		$DBConnection->add_data($TableName,"dm_hutang",GetParam("manager_operasi",""));
		$DBConnection->add_data($TableName,"dm_piutang",GetParam("manager_keuangan",""));
		
		$DBConnection->add_data($TableName,"nodetbank",GetParam("nodetbank",""));
		$DBConnection->add_data($TableName,"nodetkas",GetParam("nodetkas",""));
		
		$DBConnection->InsertGrid();
              
    }

    if (GetParam("Action","")=="Update") {
        $FormMode="Edit";    
        //FormUpdate();
        
        
        /*
        ledger='".GetParam("ledger","")."',
        prsh='".GetParam("prsh","")."',
        perush='".GetParam("perush","")."',
        perusha='".GetParam("perusha","")."',
        npwp='".GetParam("npwp","")."', 
        ktrasl='".GetParam("ktrasl","")."',
        ktrnam='".GetParam("ktrnam","")."',
        acc_manager='".GetParam("acc_manager","")."',
        treasury_manager='".GetParam("treasury_manager","")."',
        addr1='".GetParam("addr1","")."',
        addr2='".GetParam("addr2","")."',
        addr3='".GetParam("addr3","")."',
        phone='".GetParam("phone","")."',
        fax='".GetParam("fax","")."',
        nodetbank='".GetParam("nodetbank","")."',
        nodetkas='".GetParam("nodetkas","")."',
        nodetnd='".GetParam("nodetnd","")."',
        totnd='".GetParam("totnd","")."',
        nodetnk='".GetParam("nodetnk","")."',
        totnk='".GetParam("totnk","")."',
        nomemo='".GetParam("nomemo","")."',
        nomemx='".GetParam("nomemx","")."',
        */
        $DBConnection->dbc->query("UPDATE ".$TableName." SET 
        salrl='".GetParam("salrl","")."',
        salrlytd='".GetParam("salrlytd","")."',
        skdstm='".GetParam("skdstm","")."',
        skdgrphut='".GetParam("skdgrphut","")."',
        skdngrhut='".GetParam("skdngrhut","")."',
        taxcode='".GetParam("taxcode","")."',
        taxprefix='".GetParam("taxprefix","")."',
        tglpkp='".GetParam("tglpkp","")."',
        selkurs='".GetParam("selkurs","")."',
        bs='".GetParam("bs","")."',
        kw='".GetParam("kw","")."',
        email='".GetParam("email","")."',
        url='".GetParam("url","")."',
        ttdbs1='".GetParam("ttdbs1","")."',
        ttdbs2='".GetParam("ttdbs2","")."',
        ttdbs3='".GetParam("ttdbs3","")."',
        ttd_setuju_jo_kur='".GetParam("ttd_setuju_jo_kur","")."',
        ttd_setuju_jo_lbh='".GetParam("ttd_setuju_jo_lbh","")."',
        ttd_tgjwb_jo_kur='".GetParam("ttd_tgjwb_jo_kur","")."',
        ttd_tgjwb_jo_lbh='".GetParam("ttd_tgjwb_jo_lbh","")."',
        ttd_setuju_survey_kur='".GetParam("ttd_setuju_survey_kur","")."',
        ttd_setuju_survey_lbh='".GetParam("ttd_setuju_survey_lbh","")."',
        ttd_tgjwb_survey_kur='".GetParam("ttd_tgjwb_survey_kur","")."',
        ttd_tgjwb_survey_lbh='".GetParam("ttd_tgjwb_survey_lbh","")."',
        ttd_setuju_oth_kur='".GetParam("ttd_setuju_oth_kur","")."',
        ttd_setuju_oth_lbh='".GetParam("ttd_setuju_oth_lbh","")."',
        ttd_tgjwb_oth_kur='".GetParam("ttd_tgjwb_oth_kur","")."',
        ttd_tgjwb_oth_lbh='".GetParam("ttd_tgjwb_oth_lbh","")."',
        ttdcn='".GetParam("ttdcn","")."',
        ttddn='".GetParam("ttddn","")."',
        bankdn='".GetParam("bankdn","")."',
        ttdkw='".GetParam("ttdkw","")."',
        ttdmm='".GetParam("ttdmm","")."',
        ttdmb='".GetParam("ttdmb","")."',
        ttdmacc='".GetParam("ttdmacc","")."',
        jabtax='".GetParam("jabtax","")."',
        araging='".GetParam("araging","")."',
        apaging='".GetParam("apaging","")."',
        ttdkas1='".GetParam("ttdkas1","")."',
        ttdkas2='".GetParam("ttdkas2","")."',
        ttdkas3='".GetParam("ttdkas3","")."',
        ttdagh1='".GetParam("ttdagh1","")."',
        ttdagh2='".GetParam("ttdagh2","")."',
        ttdagp1='".GetParam("ttdagp1","")."',
        ttdagp2='".GetParam("ttdagp2","")."',
        ttdtktb1='".GetParam("ttdtktb1","")."',
        ttdtktb2='".GetParam("ttdtktb2","")."',
        jatahtktb1='".GetParam("jatahtktb1","")."',
        jatahtktb2='".GetParam("jatahtktb2","")."',
        ttdbkbb1='".GetParam("ttdbkbb1","")."',
        ttdbkbb2='".GetParam("ttdbkbb2","")."',
        jatahbkbb1='".GetParam("jatahbkbb1","")."',
        jatahbkbb2='".GetParam("jatahbkbb2","")."',
        ppn_debtur='".GetParam("ppn_debtur","")."',
        ppn_skada='".GetParam("ppn_skada","")."',
        adm_skada='".GetParam("adm_skada","")."',
        ttnbankdn='".GetParam("ttnbankdn","")."',
        ttdbankdn='".GetParam("ttdbankdn","")."',
        kodecbg_pjk='".GetParam("kodecbg_pjk","")."',
        bag_operasi='".GetParam("bag_operasi","")."',
        bag_traffic='".GetParam("bag_traffic","")."',
        manager_marketing='".GetParam("manager_marketing","")."',
        manager_operasi='".GetParam("manager_operasi","")."',
        manager_keuangan='".GetParam("manager_keuangan","")."',
        dm_hutang='".GetParam("dm_hutang","")."',
        dm_piutang='".GetParam("dm_piutang","")."',
        nodetbank ='".GetParam("nodetbank","")."',
        nodetkas = '".GetParam("nodetkas","")."'
        WHERE profile_id='".$profileId."'");
        
                
    }

    if (GetParam("Action","")=="Delete") {
        $FormMode="Edit";
        $FormMode="Edit";
        FormDelete();
    }

    if (GetParam("Action","")=="Cancel") {
        $FormMode="Edit";
        FormCancel();
    }
    
    
    $DBConnection->dbc->query("DELETE FROM tblreportsign WHERE profile_id='".$profileId."' ");
	FormSave("tblreportsign", "staff_id", array("profile_id"=>"","staff_id"=>"","assignment"=>"","report_kind"=>"","range_left"=>"number","range_right"=>"number"),"","profile_id",$profileId);
	$DBConnection->InsertGrid();
    

	header("Location: $FileName?PageMode=Form");
	exit;
    
}



//Field Verification
$Verification  = "Enable";
$SessionVar    = md5(microtime());
//$SessionVar    = "Stuck";

//************ SET FORM MODE ***************
$SQLCount = "SELECT COUNT(*) FROM $TableName";
$isExist = $DBConnection->dbc->get_var($SQLCount);

if ($isExist) $FormMode = "Edit";
else $FormMode = "Add";

//******* DATABASE FORM QUERY ***********
$SQL = "SELECT * FROM $TableName ";

if ($isExist && $FormMode="Edit") {
    $SQLForm = $SQL;
    //"WHERE ".PRIMARY_KEY."='".GetParam(PRIMARY_KEY,"")."'";
}

//******* SHOW FORM CONTENT *********
$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);

if ($isExist) $TBS->MergeBlock("blv","mysql",$SQLForm);
else $TBS->MergeBlock("blv",$FormCValue);
//HTML Name
$TBS->MergeBlock("bln",$FormCName);
//HTMLGrid

$SQLGrid = "SELECT * FROM tblreportsign as a  
            LEFT JOIN ".$DBGeneral.".tblstaff as b on a.staff_id = b.staff_id 
            ORDER BY report_sign_id ASC ";

$Result  = $DBConnection->dbc->get_results($SQLGrid,ARRAY_A);
$Result  = BuildEmptyGrid ($FormFields, $MaxGrid, $Result);

$TBS->MergeBlock("blg",$Result);

$TBS->Show();

//*************** ACTION FUNCTION *******************
function FormInsert() {
    global $DBConnection,$FileName;
    $DBConnection->Insert();
}

function FormUpdate() {
    global $DBConnection,$FileName,$profileId;    
    $DBConnection->Update(GetOfficeID());    
}

function FormDelete() {
    global $DBConnection,$FileName,$profileId;
    $DBConnection->Delete("$TableName",PRIMARY_KEY);
}

function FormCancel () {
    global $FileName;
    header("Location: $FileName");
    exit;
}


function FormSave($table, $keyField, $FormFields, $SpecialCase="", $PrimaryKey="", $PrimaryKeyVal="") {
        global $FileName, $MaxGrid, $DBConnection;
        $TotalData = $MaxGrid * 2;
        $NewDataStart = GetParam("NewData","0");
        $NewDataStart = 1;

        

        for ($a=1;$a<=$MaxGrid;$a++) {
	    //Begin For

            
            if (GetParam($keyField.$a,""))
            {
			//Begin Check if Empty Keyfield	
                                 
                foreach ($FormFields as $key=>$formatting) {
                        
						$value = GetParam($key.$a,"");
                        
                                                
						if ($formatting=="number") 
						{
						   $value = str_replace(",","",$value);
						}
						
						if ($formatting=="date")
						{
								list($day,$month,$year) = explode("/",$value);
								$value = "$year-$month-$day";
						}
						//Inser if the key is equal primary key, then set valu = primary header
						
										   
						if($key == "$PrimaryKey")
						{						   
						   $DBConnection->add_data($table,$key,$PrimaryKeyVal);
						}
																			   
					   
						else
						{						   
						   $DBConnection->add_data($table,$key,$value);  
						}
						
                       
                }
                
                                
                                
                $rate = StrToNumber(GetParam("rate",""));
                $jmh  = StrToNumber(GetParam("jumlah".$a,""));

                // Add by Chandra (15 May 2009)
                $cash_type = GetParam("cash_type","");
                //var_dump($cash_type); exit;



                if($cash_type) $DBConnection->add_data($table,'cash_type',$cash_type);
                // end update


                if (strlen($SpecialCase)) 
                {
                        foreach ($SpecialCase as $key => $value) 
                        {
                                //print "add_data($table,$key,$value);<br>";
                                $DBConnection->add_data($table,$key,$value);
                        }
                }

//                print "$a < $NewDataStart<br>";
                if ($a < $NewDataStart) 
                {
                         if (strlen($PrimaryKey)) 
                         {
                            $DBConnection->UpdateGrid($PrimaryKey,GetParam($PrimaryKey.$a,""));
                         } 
                         else 
                         {
                            $DBConnection->UpdateGrid($keyField,GetParam($keyField.$a,""));
                         }
                } 
                
                else 
                {
                        $DBConnection->InsertGrid();
//                        $DBConnection->dbc->debug();
                }
            
            
            //End Check if Empty Keyfield
            }
            
            
        //End For    
        }
        $DBConnection->ClearData();
}


//*************** USER FUNCTION *******************
function buildsqlfield ($table, $arrayfield) {
        global $DBConnection;
        for ($x=0;$x<count($arrayfield);$x++) {
            $DBConnection->add_htmlfield($table,$arrayfield[$x],GetParam($arrayfield[$x],""));
        }

}
?>
